package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageReservedListActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageReservedOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageReservedListBean;

import java.util.List;

/**
 * 作者：wsy on 2017/12/26 09:31
 * <p>
 * 邮箱：445979770@qq.com
 */

public class RefundStorageReservedListAdapter extends BaseAdapter {
    private Context mContext;
    private List<RefundStorageReservedListBean.ReferBodyBean> mReservedRefundStorageList;
    private LayoutInflater inflater;
    private RefundStorageReservedListActivity.RefundStorageReservedListener mRefundStorageReservedListener;
    private String mReservedNumber;

    public RefundStorageReservedListAdapter(Context context, List<RefundStorageReservedListBean.ReferBodyBean> reservedRefundStorageList, String reservedNumber, RefundStorageReservedListActivity.RefundStorageReservedListener refundStorageReservedListener) {
        mContext = context;
        mReservedRefundStorageList = reservedRefundStorageList;
        mRefundStorageReservedListener = refundStorageReservedListener;
        inflater = LayoutInflater.from(context);
        mReservedNumber = reservedNumber;

    }

    @Override
    public int getCount() {
        return mReservedRefundStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReservedRefundStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_refund_storage_reserved_list_first_item, null);
            EditText reservedNumber = (EditText) convertView.findViewById(R.id.reservedNumber);
            Button search = (Button) convertView.findViewById(R.id.search);
            reservedNumber.setText(mReservedNumber);
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mRefundStorageReservedListener.onNoDoubleClickListener(reservedNumber.getText().toString());
                }
            });
        } else {
            RefundStorageReservedListBean.ReferBodyBean saleRefundStorageItem = mReservedRefundStorageList.get(position - 1);
            RefundStorageReservedListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_refund_storage_reserved_list, null);
                viewHolder = new RefundStorageReservedListAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (RefundStorageReservedListAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, saleRefundStorageItem);
        }
        return convertView;
    }

    private void initView(RefundStorageReservedListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.reservedNumber = (TextView) view.findViewById(R.id.reservedNumber);
        viewHolder.costObject = (TextView) view.findViewById(R.id.costObject);
        viewHolder.costObjectDescription = (TextView) view.findViewById(R.id.costObjectDescription);
    }

    private void initData(Context context, RefundStorageReservedListAdapter.ViewHolder viewHolder, View view, RefundStorageReservedListBean.ReferBodyBean saleRefundStorageItem) {
        viewHolder.reservedNumber.setText(saleRefundStorageItem.getRefer_receipt_code());
        viewHolder.costObject.setText(saleRefundStorageItem.getReserve_cost_obj_code());
        viewHolder.costObjectDescription.setText(saleRefundStorageItem.getReserve_cost_obj_name());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, RefundStorageReservedOverviewActivity.class);
                intent.putExtra(RefundStorageExtra.RESERVED_NUMBER, viewHolder.reservedNumber.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView reservedNumber, costObject, costObjectDescription;
    }
}
